package com.wn.class30.configuration;


import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.shiro.codec.Base64;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.CookieRememberMeManager;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/** 
* @author 周豪 E-mail: 
* @version 创建时间：2020年3月7日 下午2:01:44 
* 类说明 :shiro 配置 spring注入
*/
@Configuration
public class ShiroConfig {

	/* spring注册U
	 * serRealm */
	@Bean
	public UserRealm userRealm() {
		return new UserRealm();
	}
	
	@Bean
	public CookieRememberMeManager cookieRememberMeManager() {
	//实例化rememberme管理器
	CookieRememberMeManager cookieRememberMeManager = new CookieRememberMeManager();
	//定义Cookie cookie的名字为rememberMe
	SimpleCookie cookie = new SimpleCookie("rememberMe");
	//定义Cookie的有效时间(s)
	cookie.setMaxAge(259200);
	//将cookie设置到rememberme管理器中
	cookieRememberMeManager.setCookie(cookie);
	//设置cookie的值的加密密钥（设置用户数据序列化以后采用的加密密钥）
	cookieRememberMeManager.setCipherKey(Base64.decode("6ZmI6I2j5Y+R5aSn5ZOlAA=="));
	return cookieRememberMeManager;
	}

// 初始化安全管理器
@Bean
public SecurityManager securityManager() {
	DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
	// 注册领域
	securityManager.setRealm(userRealm());
	securityManager.setRememberMeManager(cookieRememberMeManager());
	return securityManager;
}

//初始化Shiro过滤器 在该顾虑器中对用户登录状态进行判断 对资源地址进行限制
	@Bean
	public ShiroFilterFactoryBean shiroFilterFactoryBean() {
		// 实例化Filter工厂
		ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
		// 注册securityManager
		// 例如：rememberme会在过滤器中将cookie中的数据反序列化然后存储到securityManager，
		shiroFilterFactoryBean.setSecurityManager(securityManager());
		// 设置Shiro过滤器过滤规则
		Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
		// anon:所有的url都可以不登陆的情况下访问
		// authc:所有url都必须认证通过才可以访问
		// user:表示支持rememberme
		//如果不满足上方所有的规则 则需要进行登录验证
		filterChainDefinitionMap.put("/weixin/**", "anon");
		 filterChainDefinitionMap.put("/houtai/js/**", "anon");
		filterChainDefinitionMap.put("/houtai/css/**", "anon");
		filterChainDefinitionMap.put("/houtai/font/**", "anon");
		filterChainDefinitionMap.put("/houtai/img/**", "anon");
		filterChainDefinitionMap.put("/houtai/img/commentImgs/**", "anon");
		filterChainDefinitionMap.put("/houtai/layuiadmin/**", "anon");
		filterChainDefinitionMap.put("/houtai/page/adminuser/login.html", "anon");
		filterChainDefinitionMap.put("/houtai/page/adminuser/forget.html", "anon");
		filterChainDefinitionMap.put("/houtai/adminuser/login", "anon");
		filterChainDefinitionMap.put("/houtai/adminuser/sendVercode", "anon");
		filterChainDefinitionMap.put("/houtai/adminuser/checkVercode", "anon");
		filterChainDefinitionMap.put("/houtai/adminuser/updatepwd", "anon");
		filterChainDefinitionMap.put("/houtai/adminuser/changePwd", "anon");
		filterChainDefinitionMap.put("/houtai/adminuser/checkOldPwd", "anon");
		filterChainDefinitionMap.put("/houtai/page/deploy/join/getAboutus", "anon");
		filterChainDefinitionMap.put("/houtai/page/deploy/join/getJoinImgCases", "anon");
		filterChainDefinitionMap.put("/houtai/page/deploy/join/getJoinImgMeals", "anon");
		filterChainDefinitionMap.put("/houtai/page/franchisees/insert", "anon");
		filterChainDefinitionMap.put("/houtai/page/deploy/join/getJoinImgHeads", "anon");
		filterChainDefinitionMap.put("/MP_verify_S31GIEbSzaTLx5HW.txt", "anon");
		filterChainDefinitionMap.put("/houtai/logout", "logout");
		filterChainDefinitionMap.put("/houtai/**", "user");
		//未登录时重定向的网页地址
		shiroFilterFactoryBean.setLoginUrl("/houtai/page/adminuser/login.html");
		//将地址过滤规则设置到Filter工厂中
		shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
		return shiroFilterFactoryBean;
	}
	
	//通知
	@Bean
	public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor() {
		AuthorizationAttributeSourceAdvisor advisor=new AuthorizationAttributeSourceAdvisor();
	    advisor.setSecurityManager(securityManager());
	    return advisor;
	}
	@Bean
	public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator(){
		DefaultAdvisorAutoProxyCreator app=new DefaultAdvisorAutoProxyCreator();
	    app.setProxyTargetClass(true);
	    return app;
	}

}
